var window_flag = 0;
var chat_window_zIndex;
// 开窗 --在线客服使用
function openDialog() {
	if (window_flag == 0) {
		getLoginInfo();
	} else {
		layer.alert('您已打开会话窗口', {
			icon : 0,
			zIndex : layer.zIndex + chat_window_zIndex
		});
	}
}

$(function () {
    if (typeof window.addEventListener != "undefined") {
        window.addEventListener('message', function (e) {
            handleMessageEvent(e);
        }, false);
    } else if (window.attachEvent) {
        window.attachEvent('onmessage', function (e) {
            handleMessageEvent(e);
        });
    }
})

function handleMessageEvent(e) {
     //if (e.source != window.parent) return;
     var jsonData = $.parseJSON(e.data);
     if (jsonData.key == 'closeFrame') {
    	 closeWindow();
     }
}

function openWindow(url) {
	layer.open({
		id : 'visitor_chat_window',
		type : 2,
		title : [ '在线咨询', 'font-size:16px;font-weight:bold;' ],
		area : [ '1100px', '700px' ],
		maxmin : true,
		content : [ url, 'no' ],
		shade : 0,
		anim : 4, // 0 默认 、1V字渐入、 2从底部向上、3回字渐入、4翻页、5淡入、6抖动
		zIndex : layer.zIndex, // 重点1
		success : function(layero, index) {
			window_flag = 1;
			$('#visitor_chat_window').parent('div.layui-layer')
					.css('min-width', '1024px');
			var zIndex = $('#visitor_chat_window').parent(
					'div.layui-layer').css('zIndex');
			chat_window_zIndex = parseInt(zIndex);
		},
		cancel : function(index, layero) {
			closeWindow();
		},
		min : function(layero) {
			$('#visitor_chat_window').parent('div.layui-layer')
					.css('min-width', '');
		},
		restore : function(layero) {
			$('#visitor_chat_window').parent('div.layui-layer')
					.css('min-width', '1024px');
		},
		full : function(layero) {
			$('#visitor_chat_window').parent('div.layui-layer')
					.css('min-width', '1024px');
		}
	});
	return false;
}

function closeWindow() {
	window_flag = 0;
	layer.closeAll();
}

function getLoginInfo() {
	var url = 'http://kf.ane56.com/chat/chatClient/chatbox.jsp?companyID=9027&configID=9&pagereferrer=%E5%BF%AB%E8%BF%90%E5%AE%98%E7%BD%91PC&live800_referer=KYPCGW';
	//l = 'http://kftest.ane56.com/chat/chatClient/chatbox.jsp?companyID=9027&configID=9&pagereferrer=%E5%BF%AB%E8%BF%90%E5%AE%98%E7%BD%91PC&live800_referer=KYPCGW';
    $.ajax({
        type: "GET",
        url: "../user/getLoginInfo",
        dataType: "json",
        success: function (data) {
            if (data.result) {
                url += "&info=" + data.info;
            }
            openWindow(url);
        },
        error: function (result) {
        }
    });
}


